package com.nxy.cpfsfe.busi.bbatproc;

import com.adtec.starring.datapool.CompSDO;
import com.adtec.starring.datapool.EPOper;
import com.adtec.starring.exception.BaseException;
import com.adtec.starring.struct.dta.DtaInfo;
import com.adtec.starring.util.SpringUtil;
import com.nxy.cpfsfe.config.CpfsFeAllConfig;
import com.nxy.cpfsfe.enumtype.MsgCdOrTpEnum;
import com.nxy.cpfsfe.enumtype.RespCd;
import com.nxy.cpfsfe.enumtype.StatEnum;
import com.nxy.cpfsfe.mybatis.mapper.ToOrgMapper;
import com.nxy.cpfsfe.mybatis.mapper.TpRefrshTblnmMapper;
import com.nxy.cpfsfe.mybatis.pojo.ToOrg;
import com.nxy.cpfsfe.mybatis.pojo.TpRefrshTblnm;
import com.nxy.cpfsfe.service.CpfsService;
import com.nxy.cpfsfe.util.DateUtil;
import com.nxy.cpfsfe.util.XmlUtil;
import org.apache.ibatis.session.SqlSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;

import java.util.Date;
/**
 * 
 * Description: 处理NPS系统登录应答NPS.806
 * @author wp
 * @date 2019年9月12日
 */
public class BBatLgnRspSrvBean implements CpfsService{
	
	private final static Logger LOGGER = LoggerFactory.getLogger(BBatLgnRspSrvBean.class);
	SqlSession sqlSession = (SqlSession) SpringUtil.getBean("mybatisSqlSession");	
	
	@Override
	public void doService() {
		String bizMsg=null;
		String tpId = DtaInfo.getInstance().getTpId();		
		// 获取输入
		CompSDO inputSdo = (CompSDO) EPOper.getCompSDO(tpId, "ReqBody"); // 获取输入接口		
		bizMsg = (String) inputSdo.getValue("BizMsg")!=null?(String) inputSdo.getValue("BizMsg"):"";
		//String sndAppCd = (String) EPOper.get(tpId, "ReqBean[0].head[0].sndAppCd");
		LOGGER.info("接收到输入BizMsg报文数据:[{}]" + bizMsg);

		CpfsFeAllConfig.jedisCluster.publish(XmlUtil.getValByXPath(bizMsg,"/transaction/header/msg/refSndMbrCd")+XmlUtil.getValByXPath(bizMsg,"/transaction/header/msg/refSeqNb"),bizMsg);
		LOGGER.info("redis消息发布者={}",(XmlUtil.getValByXPath(bizMsg,"/transaction/header/msg/refSndMbrCd")+XmlUtil.getValByXPath(bizMsg,"/transaction/header/msg/refSeqNb")));	
		
		try {
			String msgCd =XmlUtil.getValByXPath(bizMsg,"/transaction/header/msg/msgCd");
			if(MsgCdOrTpEnum.MSGCD_806.getCode().equals(msgCd)){
				LOGGER.debug("NCS登录应答报文标识号应为:[{}]，此报文标识号为：[{}]",MsgCdOrTpEnum.MSGCD_806.getCode(),msgCd);
				ToOrg  toOrg = new ToOrg(); 
				String loginOprTp =XmlUtil.getValByXPath(bizMsg,"/transaction/body/LoginRspn/LoginRspnInf/LoginOprTp");
				String prcSts =XmlUtil.getValByXPath(bizMsg,"/transaction/body/LoginRspn/LoginRspnInf/PrcSts");
				//String pjctInf =XmlUtil.getValByXPath(bizMsg,"/transaction/body/LoginRspn/LoginRspnInf/RjctInf");
				//String orgnlMsgId =XmlUtil.getValByXPath(bizMsg,"/transaction/body/LoginRspn/OrgnlGrpInf/OrgnlMsgId");
				String orgBNo =XmlUtil.getValByXPath(bizMsg,"/transaction/body/LoginRspn/GrpHdr/InstdPty/InstdDrctPty"); 
				toOrg.setOrgBNo(orgBNo);				
				if(StatEnum.LOGINOPRTP_OT0.getCode().equals(loginOprTp)&&StatEnum.STAR_PR02.getCode().equals(prcSts)){
					toOrg.setNcsSta("OT00");  //登录
				}else if(StatEnum.LOGINOPRTP_OT1.getCode().equals(loginOprTp)&&StatEnum.STAR_PR02.getCode().equals(prcSts)){
					toOrg.setNcsSta("OT01");  //退出
				}
				toOrg.setUpdTm(new Date());
				int toOrgUpdateNum = sqlSession.getMapper(ToOrgMapper.class).update(toOrg);
				if(toOrgUpdateNum == 0){
					LOGGER.error("更新数据异常：应用机构信息表TO_ORG,更新数据条数不符合预期：应用机构行号："+orgBNo);
					return;
				}
				//刷新时间戳
				TpRefrshTblnm tblnm = new TpRefrshTblnm();
				tblnm.setTblNm("to_org");
				tblnm.setUpdtTime(DateUtil.getDateTime());
				sqlSession.getMapper(TpRefrshTblnmMapper.class).update(tblnm);
				LOGGER.debug("修改机构表的ncsSta值成功，刷新时间戳成功");
				
			}
		} catch (Throwable e) {
			LOGGER.error("NCS登录应答报文处理失败,原报文信息是："+bizMsg,e);
			throw new BaseException(RespCd.NCPS5006,e);
		}


		CompSDO outputSdo = EPOper.getCompSDO(tpId, "RspBody"); // 获取输出接口
		EPOper.put(tpId, "RspBean[0].head[0].rspsCd", RespCd.NCPI0000);
		EPOper.put(tpId, "RspBean[0].head[0].respsInfoDesc", "交易成功");
		outputSdo.setValue("BizMsg", null);
	}

}
